#include <embios/utils/Bench.h>
#include <embios/kernel/Scheduler.h>
#include <xdc/runtime/System.h>

Int count = 3;

typedef struct {
    Scheduler_Fiber fiber;
    Scheduler_Event event;
} State;

State gblA, gblB;

Void reactorA( Ptr me, Scheduler_Signal sig )
{
    System_printf("reactorA enter (%d)\n", count--);

    if (count > 0) {
        Bench_begin("A=>B");
        Scheduler_postEvent(&gblB.event);
    }
    else {
        System_exit(0);
    }
    System_printf("reactorA leave\n");
}

Void reactorB( Ptr me, Scheduler_Signal sig )
{
    Bench_end();
    System_printf("reactorB enter\n");
    Scheduler_postEvent(&gblA.event);
    System_printf("reactorB leave\n");
}

Int main()
{
    System_printf("test1\n");

    Scheduler_initFiber(&gblA.fiber, 0);
    Scheduler_initEvent(&gblA.event, offsetof (State, event));

    Scheduler_setReactor(&gblA.fiber, reactorA);
    Scheduler_setSignal(&gblA.event, 0);

    Scheduler_initFiber(&gblB.fiber, 1);
    Scheduler_initEvent(&gblB.event, offsetof (State, event));

    Scheduler_setReactor(&gblB.fiber, reactorB);
    Scheduler_setSignal(&gblB.event, 0);

    Scheduler_postEvent(&gblA.event);
    Scheduler_start();

    return 0;
}
